package com.thx.privilege.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.thx.common.orm.HibernateBaseDao;
import com.thx.common.util.AppContext;
import com.thx.common.util.ThreadLocalContext;
import com.thx.privilege.model.PrivilegeRole;

@Repository
public class PrivilegeRoleDao extends HibernateBaseDao<PrivilegeRole> {

	@SuppressWarnings("unchecked")
	public List<PrivilegeRole> getPrivileges(String roleId, String priviType) {
		String comId = (String) ThreadLocalContext
				.get(AppContext.CURRENT_COM_ID);
		String hql = "from PrivilegeRole where roleId=? and type=? and comId=?";
		return createQuery(hql, roleId, priviType, comId).list();
	}

	@SuppressWarnings("unchecked")
	public PrivilegeRole getPrivileges(String roleId, String objId,
			String priviType) {
		String comId = (String) ThreadLocalContext
				.get(AppContext.CURRENT_COM_ID);
		String hql = "from PrivilegeRole where roleId=? and objId=? and type=? and comId=?";
		List<PrivilegeRole> prs = createQuery(hql, roleId, objId, priviType,
				comId).list();
		if (prs != null && prs.size() > 0) {
			return prs.get(0);
		}
		return null;
	}

	public void delPriRole(String objId, String type) {
		String hql = "delete from PrivilegeRole where objId=? and type=?";
		createQuery(hql, objId, type).executeUpdate();
	}
}
